import { modal } from 'vue-strap'
import { showMsg, codeAddBatch } from '../../../../vuex/action'
import DatePicker from 'vue-datepicker'
import getDatePickerOption from '../../../../util/date-picker-option'
export default {
  name: 'import-code',
  props: ['prizeId'],
  data() {
    return {
      datePickerOption: getDatePickerOption(),
      showMiniCodeImport: false,
      title: '批量添加兑换码',
      update: {
        id: +this.prizeId,
        codes: '',
        expireTime: ''
      }
    }
  },
  vuex: {
    // getters: {
    //   prizeId: state => state.prizeStoreId
    // },
    actions: {
      xShowMsg: showMsg,
      xCodeAddBatch: codeAddBatch
    }
  },
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  ready() {
    this.datePickerOption.type= 'day'
    this.datePickerOption.format = 'YYYY-MM-DD'
  },
  methods: {
    addCode() {
      this.showMiniCodeImport = true
      this.update = {
        id: +this.prizeId,
        codes: '',
        expireTime: ''
      }
    },
    submit() {
      var codes = this.update.codes
      // 验证是否输入兑换码
      if (/^[^,|，]/.test(codes) && codes.length > 0) {
        var codeArr = codes.split(/[ ]*(?:,|，)[ ]*/)
      } else {
        return this.xShowMsg({
          type: 'danger',
          content: '请输入格式正确的奖品兑换码'
        })
      }
      if ((this.update.expireTime + '').length === 0) {
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的均为必填/必选项'
        })
      }
      this.update.codes = codeArr.join(',')
      // 请求接口
      this.xCodeAddBatch(this.update)
        .then(() => {
          this.showMiniCodeImport = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
